<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<meta charset="UTF-8">
	<title>README</title>
	<style>
		body {
			margin-bottom: 50px;
		}
		.list_indent {
			padding-left: 2em;
		}
		code {
			padding-left: 2px;
			padding-right: 2px;
			background: hsl(220, 80%, 90%);
		}
		.implemented {
			font-size: 70%;
			padding-left: 2px;
			padding-right: 2px;
			background: hsl(120, 80%, 90%);
		}
		.planned {
			font-size: 70%;
			padding-left: 2px;
			padding-right: 2px;
			background: hsl(60, 80%, 90%);
		}
	</style>
</head>
<body>
	<h1>Compilers</h1>
	<h4><i>Luuk van Sloun &amp; Atisha Ribeiro</i></h4>
	<br>
	<h3>Project Structure</h3>
	<ul>
		<li><b>Grammar</b></li>
		<span class="list_indent">
			The grammar is specified in <code>C_Grammar.g4</code>.
		</span>
		<li><b>AST</b></li>
		<div class="list_indent">
			There are 2 classes located in <code>AST.py</code>: The <code>AST</code> class and the <code>GrammarListener</code> class.<br/>
			<code>AST</code> is just a node class that has children nodes. Each node stores 3 values:<br/>
			<ul>
				<li><code>name</code>: specifies the node type (e.g. function)</li>
				<li><code>value</code>: specifies the node value (e.g. main)</li>
				<li><code>type</code>: specifies the node type (e.g. int)</li>
			</ul>
			The class also contains a <code>to_dot()</code> function to visualise the AST.<br/>
			<code>GrammarListener</code> contains an AST object and fills it as it traverses the derivation tree generated by ANTLR.<br/>
		</div>
		<li><b>Symbol Table</b></li>
		<div class="list_indent">
			The <code>SymbolTable</code> class is located in <code>SymbolTable.py</code>.<br/>
			It uses DFS to traverse the AST and fill in the symbol table. While traversing, it checks if variables are declared only once and if variables used have been declared previously.
			If these checks fail, an exception is thrown.<br/>
			The class also contains a <code>to_dot()</code> function to visualise the symbol table.<br/>
		</div>
		<li><b>Code Generation</b></li>
		<div class="list_indent">
			The <code>CodeGenerator</code> class is located in <code>CodeGenerator.py</code>.<br/>
			Traverses the AST recursively with DFS and writes the LLVM IR code to a file.
		</div>
	</ul>

	<h3>Features</h3>
	<ul>
		<li><b>Types</b> <span class="implemented">implemented</span></li>
		<li><b>Import</b> <span class="implemented">implemented</span></li>
		<div class="list_indent">
			<code>printf</code> and <code>scanf</code> are declared when <code>#include &ltstdio.h&gt</code> is specified.
		</div>
		<li><b>Reserverd Words</b> <span class="implemented">implemented</span></li>
		<li><b>Local and global variables</b> <span class="implemented">implemented</span></li>
		<li><b>Comments</b> <span class="implemented">implemented</span></li>
		<li><b>Operations</b> <span class="implemented">implemented</span></li>
		<ul>
			<li><b>Unary Operations <code>+</code> and <code>-</code></b> <span class="implemented">implemented</span></li>
			<li><b>Comparison Operators</b> <span class="implemented">implemented</span></li>
		</ul>
		<li><b>Functions</b> <span class="implemented">implemented</span></li>
		<div class="list_indent">
			exception thrown when there is no return in a non-void function, or when there is no main() present in the code.
		</div>
		<li><b>Arrays</b> <span class="implemented">implemented</span></li>
		<li><b>Conversions</b> <span class="implemented">implemented</span></li>
		<div class="list_indent">
			casts implicitly when there is a type mismatch
		</div>
	</ul>

	<h3>Optimisations</h3>
	<ul>
		<li><b>Unreachable/Dead code</b> <span class="implemented">implemented</span></li>
		<div class="list_indent">
			statements that do not change variables (like <code>a + 2;</code>) are also not generated.
		</div>
	</ul>

	<h3>Usage</h3>
	<ul>
		<li><b>Builing</b></li>
		<div class="list_indent">
			To build the C compiler classes type <code>bash build</code>.<br/>
			This will also compile and execute (with <code>lli</code>) an example file <code>c_prog</code>.
		</div>
		<li><b>Testing</b></li>
		<ul>
		<li>Working test files</li>
		<div class="list_indent">
			To run the files that showcase the implemented features, type <code>bash test</code>.
		</div>
		<li>Error test files</li>
		<div class="list_indent">
			These files showcase exceptions we have implemented. Since they throw an exception, they have to be executed one by one.
		</div>
		</ul>
	</ul>


</body></html>